home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 5 / Skunkware 5.iso / src / Tools / MotifApp / Extras / TicTacToe / Board.h < prev    next >
Encoding:
C/C++ Source or Header  |  1995-05-03  |  2.2 KB  |  73 lines

  1. ///////////////////////////////////////////////////////////////////////////////
  2. //////////////////////////////////////////////////////////////////////////////
  3. //         This example code is from the book:
  4. //
  5. //           Object-Oriented Programming with C++ and OSF/Motif
  6. //         by
  7. //           Douglas Young
  8. //           Prentice Hall, 1992
  9. //           ISBN 0-13-630252-1    
  10. //
  11. //         Copyright 1991 by Prentice Hall
  12. //         All Rights Reserved
  13. //
  14. //  Permission to use, copy, modify, and distribute this software for 
  15. //  any purpose except publication and without fee is hereby granted, provided 
  16. //  that the above copyright notice appear in all copies of the software.
  17. ///////////////////////////////////////////////////////////////////////////////
  18. //////////////////////////////////////////////////////////////////////////////
  19.  
  20.  
  21. /////////////////////////////////////////////////////////////
  22. // Board.h: Represent a tictactoe board
  23. /////////////////////////////////////////////////////////////
  24. #ifndef BOARD_H
  25. #define BOARD_H
  26.  
  27. // Convenient values
  28.     
  29. enum MoveStatus { validMove, illegalMove };
  30. enum markType { NOBODYYET, OO, XX, TIE };
  31.  
  32. class Board {
  33.     
  34.  protected:
  35.     
  36.     int  _state[9];        // Internal game state
  37.     int  _freeList[9];     // List used to report free squares
  38.     int  numFreeSquares(); 
  39.     int  *_winningPattern; // Pattern last tested when someone wins
  40.     static int _winningBits[8][9];
  41.     
  42.  public:
  43.     
  44.     Board();
  45.  
  46.     MoveStatus recordMove ( int, markType ); // Record an X or an O
  47.  
  48.     // Return the value stored in a particular square
  49.     
  50.     int value(int index) { return ( _state[index] ); }
  51.     
  52.     // Return number of available squares, and their indexes
  53.     
  54.     int *const freeSquares ( int& );   
  55.  
  56.     // Return the first move that could win for the given 
  57.     // player, if there is one
  58.  
  59.     int winningMove(markType);
  60.     
  61.     // Public access for winning pattern of squares
  62.     
  63.     int *const winningSquares() { return _winningPattern; }
  64.     
  65.     void clear();           // Clear and reset the board
  66.  
  67.     markType whoHasWon();   // Return code for possible winner
  68.  
  69.     virtual const char *const className() { return "Board"; }
  70. };
  71.  
  72. #endif
  73.